package org.moonshiner.controller;

import java.io.File;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.tmatesoft.svn.core.SVNAuthenticationException;
import org.tmatesoft.svn.core.SVNDirEntry;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNLogEntry;
import org.tmatesoft.svn.core.SVNLogEntryPath;
import org.tmatesoft.svn.core.SVNNodeKind;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.internal.wc.DefaultSVNAuthenticationManager;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNCommitClient;
import org.tmatesoft.svn.core.wc.SVNWCUtil;

public class Application {

	public static final String SVN_REPOSITORY_URL = "https://riouxsvn.com/svn/onlineide";

	@SuppressWarnings("deprecation")
	public void run() {

		String path = "trunk/MySvn/.classpath";

		DAVRepositoryFactory.setup();

		ISVNAuthenticationManager authenticationManager = SVNWCUtil
				.createDefaultAuthenticationManager("Roston", "msgtvp9cp");
		((DefaultSVNAuthenticationManager) authenticationManager)
				.setAuthenticationForced(true);

		SVNURL svnurl = null;
		SVNRepository repository = null;
		try {
			svnurl = SVNURL.parseURIDecoded(Application.SVN_REPOSITORY_URL);
			repository = SVNRepositoryFactory.create(svnurl);
			repository.setAuthenticationManager(authenticationManager);
			repository.testConnection();
		} catch (SVNAuthenticationException e) {
			e.printStackTrace();
		} catch (SVNException e) {
			e.printStackTrace();
			e.getCause().getClass().equals(UnknownHostException.class);
		}

		// printLogEntries(repository);

		SVNClientManager clientManager = SVNClientManager.newInstance();
		clientManager.setAuthenticationManager(authenticationManager);

		SVNCommitClient commitClient = clientManager.getCommitClient();

		try {
			commitClient
					.doCommit(
							new File[] { new File(
									"src/main/java/com/epam/training/controller/comparator/CommitHistoryComparator.java") },
							true, "commit with commitClient", false, true);
		} catch (SVNException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void printLogEntries(SVNRepository repository) {
		Collection<SVNLogEntry> logEntries = null;

		try {
			logEntries = repository.log(new String[] { "" }, null, 0, -1, true,
					true);
			System.out.println(repository.getLatestRevision());
		} catch (SVNException e) {
			e.printStackTrace();
		}

		for (SVNLogEntry logEntry : logEntries) {
			System.out.println("---------------------------------------------");
			System.out.println("revision: " + logEntry.getRevision());
			System.out.println("author: " + logEntry.getAuthor());
			System.out.println("date: " + logEntry.getDate());
			System.out.println("log message: " + logEntry.getMessage());

			if (logEntry.getChangedPaths().size() > 0) {
				System.out.println();
				System.out.println("changed paths:");
				Set changedPathsSet = logEntry.getChangedPaths().keySet();

				for (Iterator changedPaths = changedPathsSet.iterator(); changedPaths
						.hasNext();) {
					SVNLogEntryPath entryPath = (SVNLogEntryPath) logEntry
							.getChangedPaths().get(changedPaths.next());
					System.out.println(" "
							+ entryPath.getType()
							+ " "
							+ entryPath.getPath()
							+ ((entryPath.getCopyPath() != null) ? " (from "
									+ entryPath.getCopyPath() + " revision "
									+ entryPath.getCopyRevision() + ")" : ""));
				}
			}

		}
	}

	private List<SVNDirEntry> listDirs(SVNRepository repository, String path)
			throws SVNException {

		List<SVNDirEntry> dirEntries = new ArrayList<SVNDirEntry>();

		Collection<?> entries = repository.getDir(path, -1, null, dirEntries);
		Iterator<?> iterator = entries.iterator();
		while (iterator.hasNext()) {
			SVNDirEntry entry = (SVNDirEntry) iterator.next();
			System.out.println("/" + (path.equals("") ? "" : path + "/")
					+ entry.getName() + " ( author: '" + entry.getAuthor()
					+ "'; revision: " + entry.getRevision() + "; date: "
					+ entry.getDate() + ")");
		}
		return dirEntries;
	}

	private void listEntries(SVNRepository repository, String path)
			throws SVNException {
		Collection<?> entries = repository.getDir(path, -1, null,
				(Collection<?>) null);
		Iterator<?> iterator = entries.iterator();
		while (iterator.hasNext()) {
			SVNDirEntry entry = (SVNDirEntry) iterator.next();
			System.out.println("/" + (path.equals("") ? "" : path + "/")
					+ entry.getName() + " ( author: '" + entry.getAuthor()
					+ "'; revision: " + entry.getRevision() + "; date: "
					+ entry.getDate() + ")");
			if (entry.getKind() == SVNNodeKind.DIR) {
				listEntries(repository, (path.equals("")) ? entry.getName()
						: path + "/" + entry.getName());
			}
		}
	}

	public void test() {
		System.out.println("test");
	}

}
